package com.zq.product.controller;


import com.zq.common.util.Result;
import com.zq.product.dao.entity.Category;
import com.zq.product.service.ICategoryService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author 张乔
 * @since 2025-05-13
 */
@RestController
@RequestMapping("/product/category")
@Slf4j
public class CategoryController {

    @Resource
    private ICategoryService categoryService;


    @PostMapping("/save")
    public Result<String> save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success();
    }

    @PutMapping("/updateById")
    public Result<String> updateById(@RequestBody Category category) {
        categoryService.updateById(category);
        return Result.success();
    }

    @DeleteMapping("/DeleteById/{id}")
    public Result<String> DeleteById(@PathVariable("id") Long id) {

        categoryService.removeById(id);
        return Result.success();
    }
    //获取所有的一级分类
@GetMapping("/ones")
    public Result<Object> ones() {
    List<Category> onesCategory= categoryService.ones();
        return Result.successData(onesCategory);
}
    //根据父级id获取子类
    @GetMapping(value = "/findByParentId/{parentId}")
    public Result<List<Category>> findByParentId(@PathVariable Long parentId) {
        List<Category> list = categoryService.findByParentId(parentId);
        return Result.successData(list);
    }

    //导出
    @GetMapping(value = "/exportData")
    public void exportData(HttpServletResponse response) {

        categoryService.exportData(response);
    }

//    导入
@PostMapping("/mportData")
public Result<String> importData( MultipartFile file) {
    log.info("开始导入"+file.getName());
    categoryService.importData(file);
    return Result.success();
}







}
